package com.baiyang.app.ui.bookshelf;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.alibaba.fastjson.JSONArray;
import com.baiyang.app.HomeActivity;
import com.baiyang.app.R;
import com.baiyang.app.databinding.FragmentBookshelfBinding;
import com.baiyang.app.model.Article;
import com.baiyang.app.model.Author;
import com.baiyang.app.tool.LoadDataScrollController;
import com.baiyang.app.tool.MyAdapter;
import com.baiyang.app.ui.bookshelf.workList.WorkListActivity;
import com.baiyang.app.ui.read.ReadPageActivity;

import java.util.ArrayList;

public class BookshelfFragment extends Fragment implements LoadDataScrollController.OnRecycleRefreshListener {

    private BookshelfViewModel bookshelfViewModel;
    private BookshelfService service;
    private FragmentBookshelfBinding binding;

    private RecyclerView recyclerView;
    //下拉刷新
    private SwipeRefreshLayout mSwipeRefresh;
    private LoadDataScrollController mController;
    private MyAdapter<Author> myAdapter ;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        bookshelfViewModel =
                new ViewModelProvider(this).get(BookshelfViewModel.class);
        binding = FragmentBookshelfBinding.inflate(inflater, container, false);
        service = new BookshelfService(getContext(),bookshelfViewModel);
        View root = binding.getRoot();

        recyclerView = binding.recyclerView;
        mSwipeRefresh = binding.refresh;
        this.mController = new LoadDataScrollController(this);
        myAdapter = new MyAdapter<Author>(new ArrayList<>(), R.layout.item_author) {
            @Override
            public void bindView(ViewHolder holder, Author obj) {
                holder.setText(R.id.authorName,obj.getUserName());
                holder.setText(R.id.lastArticle,obj.getLastArticle());
                holder.setText(R.id.lastTime,obj.getLastTime());
                if(obj.getAvatarUrl() != null){
                    holder.setPic(R.id.avatarUrl,obj.getAvatarUrl());
                }
                holder.setmOnItemClickListener(e->{
                    Intent intent = new Intent();
                    intent.putExtra("authorId",obj.getId());
                    intent.setClass(getContext(), WorkListActivity.class);
                    startActivity(intent);
                });
            }
        };
        //设置垂直的线性布局管理器，Orientation --> VERTICAL:垂直 HORIZONTAL:水平
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        this.mSwipeRefresh.setColorSchemeColors(Color.RED,Color.GREEN,Color.BLUE);
        this.mSwipeRefresh.setOnRefreshListener(mController);
        this.recyclerView.addOnScrollListener(mController);

        initHandler();

        return root;
    }

    private void initHandler() {
        bookshelfViewModel.getFollowListResult().observe(getViewLifecycleOwner(),e->{
            String result = e.getString("result");
            if("SUCCESS".equals(result)){
                JSONArray list = e.getJSONArray("list");
                if (list != null && list.size() > 0) {
                    myAdapter.addAll(list.toJavaList(Author.class));
                }else{
                    Toast.makeText(getContext(), "没有更多了", Toast.LENGTH_SHORT).show();
                }
            }else{
                String detail = e.getString("detail");
                if(detail.equals("balance")){
                    Toast.makeText(getContext(),"余额不足请充值",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(getContext(),detail,Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        recyclerView.setAdapter(null);
        recyclerView.setAdapter(myAdapter);
        myAdapter.clear();
        service.getFollowList();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    @Override
    public void refresh() {
        //刷新的接口调
        mSwipeRefresh.postDelayed(new Runnable() {
            @Override
            public void run() {
                myAdapter.clear();
                service.getFollowList();
                mSwipeRefresh.setRefreshing(false);
                mController.setLoadDataStatus(false);
            }
        },2000);
    }

    @Override
    public void loadMore() {

    }
}